package com.youlu.campus.web.data.controller;

import com.youlu.campus.entity.ActivityInfo;
import com.youlu.campus.entity.ActivityQaInfo;
import com.youlu.campus.entity.VO.req.LogFileVO;
import com.youlu.campus.entity.VO.req.SignUpDataMigrationReq;
import com.youlu.campus.entity.VO.req.UvPvVO;
import com.youlu.campus.service.activity.ActivityService;
import com.youlu.campus.service.activity.ActivityUserRecordService;
import com.youlu.campus.service.commission.DistributorChannelSharingCommissionService;
import com.youlu.campus.service.data.impl.LogFileDataVerifyService;
import com.youlu.campus.service.data.impl.UvPvService;
import com.youlu.campus.service.rank.impl.RankingDataCheckService;
import com.youlu.campus.web.common.utils.CommonUtils;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.util.List;

@Slf4j
@RestController
public class DataController {
    @Autowired
    private UvPvService uvPvService;
    @Autowired
    private ActivityService activityService;

    @Autowired
    private LogFileDataVerifyService logFileDataVerifyService;

    @Autowired
    private RankingDataCheckService rankingDataCheckService;

    @Autowired
    private ActivityUserRecordService activityUserRecordService;

    @Autowired
    private DistributorChannelSharingCommissionService distributorChannelSharingCommissionService;

    @GetMapping(value = "/public/sync-activity-mq")
    public QResult initActivity() {
        List<ActivityInfo> list = activityService.findAll();
        list.parallelStream().forEach(activityInfo -> {
            ActivityQaInfo activityQaInfo = new ActivityQaInfo();
            activityQaInfo.setActivityId(activityInfo.getId());
            activityQaInfo.setActivityName(activityInfo.getName());
            uvPvService.doActivityInfo(activityQaInfo);
        });
        return QResult.success(null);
    }

    @PostMapping(value = "/public/puv")
    public QResult pvUv(@RequestBody @Validated UvPvVO uvPv, HttpServletRequest request, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        uvPv.setClientIp(CommonUtils.getRemoteIp(request));
        uvPv.setAppClientIp(request.getLocalAddr());
        uvPvService.doUvPv(uvPv);
        return QResult.success(null);
    }

    /*@PostMapping(value = "/public/activity/import-data")
    @ResponseBody
    public RestResult importBookPriceInfo(@RequestParam("file") MultipartFile file,
                                          @RequestParam(value = "activityId", required = false) String activityId,
                                          @RequestParam(value = "taskBizType", required = false) TaskBizType taskBizType,
                                          @RequestParam(value = "token", required = false) String token) {
        try {
            String fileName = file.getOriginalFilename();
            log.info(":>>> 上传文件为:{}", fileName);
            HashMap<String, Object> result = activityService.importActivityData(file, activityId, taskBizType);
            log.info(":>>> 批量导入活动数据结果:{}", JSON.toJSON(result));
            return RestResultBuilder.builder().success(result).build();
        } catch (BusinessException e) {
            log.error(":>>> 批量导入图书价格错误:{}", e);
            throw e;
        } catch (Exception e) {
            log.error(":>>> 批量导入图书价格错误:{}", e);
            return RestResultBuilder.builder().failure().build();
        }

    }*/

    @PostMapping(value = "/public/log/explain")
    public QResult logFileVerify(@RequestBody LogFileVO logFile) throws FileNotFoundException {
        logFileDataVerifyService.checkLogFile(logFile);
        return QResult.success(null);
    }

    @PostMapping(value = "/public/ranking/data-check")
    public QResult rankDataCheck(@RequestBody List<String> activityList) {
        rankingDataCheckService.rankingCheck(activityList);
        return QResult.success(null);
    }


    @PostMapping(value = "/public/order/data-clear")
    public QResult orderDate(@RequestBody List<String> activityList) {
        rankingDataCheckService.rankingCheck(activityList);
        return QResult.success(null);
    }

    @GetMapping("/public/signup/data/migration")
    public QResult signupDataMigration(@RequestBody SignUpDataMigrationReq signUpDataMigrationReq) {
        activityUserRecordService.signUpDataMigration(signUpDataMigrationReq);
        return QResult.success(null);
    }

    @GetMapping("/public/init/distributor-commission")
    public QResult initDistributorChannelSharingCommission() {
        distributorChannelSharingCommissionService.initCommission();
        return QResult.success(null);
    }


}
